﻿using System;
using System.Linq;
using System.Windows;

namespace Microsoft.Windows.UserControls.ViewModels
{
    public class WizardPageViewModelBase : BindableBase, IWizardPageViewModel
    {
        WizardPage.WizardPageType pageType;

        public WizardPage.WizardPageType Type
        {
            get
            {
                return pageType;
            }
            set
            {
                SetProperty(ref pageType, value);
                SetViewModelDefaultValues();
            }
        }

        public IWizardPageHeaderViewModel Header { get; internal set; }

        public IWizardPageNavigationViewModel Navigation { get; internal set; }

        public IWizardPageIndexViewModel Index { get; internal set; }

        public WizardPageViewModelBase(String title, String description)
        {
            Header = new WizardPageHeaderViewModel(title, description);
            Navigation = new WizardPageNavigationViewModel();
            Index = new WizardPageIndexViewModelBase(){ Title = title};
        }

        public WizardPageViewModelBase(String title, String description, Type pageIndexType) 
            : this(title, description)
        {
            if (pageIndexType == null)
                return;
            if (!pageIndexType.GetInterfaces().Contains(typeof(IWizardPageIndexViewModel)))
                return;
            
            Index = (IWizardPageIndexViewModel)Activator.CreateInstance(pageIndexType);

            //Header = new WizardPageHeaderViewModel(title, description);
            //Navigation = new WizardPageNavigationViewModel();
        }

        protected virtual void SetViewModelDefaultValues()
        {
            Navigation.Help.IsEnabled = false;
            Navigation.Previous.IsEnabled = true;
            Navigation.Next.IsEnabled = true;
            Navigation.Cancel.IsEnabled = true;
            Navigation.Finish.IsEnabled = true;

            switch (Type)
            {
                case WizardPage.WizardPageType.Normal:
                    Navigation.Help.Visibility = Visibility.Collapsed;
                    Navigation.Previous.Visibility = Visibility.Visible;
                    Navigation.Next.Visibility = Visibility.Visible;
                    Navigation.Cancel.Visibility = Visibility.Visible;
                    Navigation.Finish.Visibility = Visibility.Collapsed;
                    break;
                case WizardPage.WizardPageType.Welcome:
                    Navigation.Help.Visibility = Visibility.Visible;
                    Navigation.Previous.Visibility = Visibility.Collapsed;
                    Navigation.Next.Visibility = Visibility.Visible;
                    Navigation.Cancel.Visibility = Visibility.Visible;
                    Navigation.Finish.Visibility = Visibility.Collapsed;
                    break;
                case WizardPage.WizardPageType.Completion:
                    Navigation.Help.Visibility = Visibility.Collapsed;
                    Navigation.Previous.Visibility = Visibility.Collapsed;
                    Navigation.Next.Visibility = Visibility.Collapsed;
                    Navigation.Cancel.Visibility = Visibility.Collapsed;
                    Navigation.Finish.Visibility = Visibility.Visible;
                    break;
                default:
                    Navigation.Help.Visibility = Visibility.Collapsed;
                    Navigation.Previous.Visibility = Visibility.Collapsed;
                    Navigation.Next.Visibility = Visibility.Collapsed;
                    Navigation.Cancel.Visibility = Visibility.Collapsed;
                    Navigation.Finish.Visibility = Visibility.Collapsed;
                    break;
            }
        }
    }
}